<template>
	<view class="page" :data-theme="theme">
		
		<view class="wechat_login">
			<view class="img">
				<image src="../static/wechat_login.png" mode="widthFix"></image> 
			</view>
			<view class="btn-wrapper">

				<button  open-type="getPhoneNumber" scope="phoneNumber" @getphonenumber="getPhoneNumber" @error="errorPhoneNumber" class=" btn1" style="background-color: #0AE;">支付宝授权登录</button>

			</view>
		</view>
		
	</view>
</template>

<script>
	const app = getApp();
	let statusBarHeight = uni.getSystemInfoSync().statusBarHeight + 'px';
	import {mapGetters} from "vuex";
	import {
		LOGO_URL,
		EXPIRES_TIME,
		USER_INFO,
		STATE_R_KEY
	} from '@/config/cache';
	import {loginAliPhone, getUserInfo } from '@/api/user.js'
	import Routine from '@/libs/routine';
	import wechat from "@/libs/wechat";
	const BACK_URL = "login_back_url";
	
	export default {
		data() {
			return {
				isUp: false,
				phone: '',
				statusBarHeight: statusBarHeight,
				isHome: false,
				isPhoneBox: false,
				logoUrl: '',
				code: '',
				authKey: '',
				options: '',
				userInfo: {},
				codeNum: 0,
				theme:app.globalData.theme,
				getPhoneContent:'申请获取您的手机号用于注册，完成后可使用商城更多功能'
			}
		},
		components: {
		},
		onLoad(options) {
		   this.getCode()

		},
		methods: {
			getCode(n){
				// uni.showLoading({
				// 	title: '正在登录中'
				// });
				Routine.getAliCode().then(code => {
					// uni.hideLoading();
					this.code = code;
				}).catch(e => {
					console.log('e',e)
					// uni.hideLoading();
					uni.showToast({
						title: '登录失败',
						duration: 2000
					});
				})
			},
			    // 拒绝手机号
			errorPhoneNumber(e){
			  console.log('拒绝授权',e)
			  this.$util.Tips({
			  	title: '拒绝授权',
			  	icon: 'success'
			  })
		    },
			getPhoneNumber(){
				let self = this;
				my.getSetting({
				  success: res => {
				    // const { album = false, location = false } = res.authSetting;
					 // 如果已经授权手机号
					 if(res.authSetting.phoneNumber){
						 my.getPhoneNumber({
						      success: (res) => {
						        let encryptedData = res.response;
						        loginAliPhone({
						          //你的服务器地址
						 			code: self.code,
						            encryptedData: encryptedData,
						 			spread: app.globalData.spid  //获取推广人ID
						        }).then(res => {
									console.log(333,res)
									let data = res.data;
									let newTime = Math.round(new Date() / 1000);
									self.$store.commit("LOGIN", {
										'token': res.data.token
									});
									self.getUserInfo(data);
									
								}).catch( err => {
									
									self.$util.Tips({
										title: '登录失败，获取手机号失败',
										icon: 'success'
									})
									setTimeout(function(){
										return uni.switchTab({
											url:'/pages/index/index'
										})
									},1500)
								})
						      },
						      fail: (res) => {
						        console.log(res);
								self.$util.Tips({
									title: '登录失败，获取手机号失败',
									icon: 'success'
								})
								setTimeout(function(){
									return uni.switchTab({
										url:'/pages/index/index'
									})
								},1500)
						      },
						    });
						 
					 } else {
						 
						 self.$util.Tips({
						 	title: '您未授权手机号，请删除小程序重新加入',
						 	icon: 'success'
						 })
						 setTimeout(function(){
						 	return uni.switchTab({
						 		url:'/pages/index/index'
						 	})
						 },1500)
						 
					 }
				
				  },
				  fail: err =>{
					  self.$util.Tips({
					  	title: '登录失败',
					  	icon: 'success'
					  })
					  setTimeout(function(){
					  	return uni.switchTab({
					  		url:'/pages/index/index'
					  	})
					  },1500)
				  }
				});
				 
			},
			getUserInfo(data){
				this.$store.commit("SETUID", data.uid); 
				getUserInfo().then(res => {
					this.$store.commit("UPDATE_USERINFO", res.data);
					// let backUrl = this.$Cache.get(BACK_URL) || "/pages/index/index";
					// if (backUrl.indexOf('/pages/alipay_login/index') !== -1) { 
					// 	backUrl = '/pages/index/index';
					// }
					// uni.switchTab({
					// 	url: backUrl
					// });
					uni.navigateBack()
				})
			},
		}
		
	}
</script>

<style lang="scss">
	page {
		background: #fff;
		height: 100%;
	}
    
	.page {
		background: #fff;
		height: 100%;
	}

	.wechat_login {
		padding: 72rpx 34rpx;

		.img image {
			width: 100%;
		}

		.btn-wrapper {
			margin-top: 86rpx;
			padding: 0 66rpx;

			button {
				width: 100%;
				height: 86rpx;
				line-height: 86rpx;
				margin-bottom: 40rpx;
				border-radius: 120rpx;
				font-size: 30rpx;

				&.btn1 {
					color: #fff;
				}

				&.btn2 {
					color: #666666;
					border: 1px solid #666666;
				}
			}
		}
	}

	.title-bar {
		position: relative;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 36rpx;
	}

	.icon {
		position: absolute;
		left: 30rpx;
		top: 0;
		display: flex;
		align-items: center;
		justify-content: center;
		width: 86rpx;
		height: 86rpx;

		image {
			width: 50rpx;
			height: 50rpx;
		}
	}
</style>
